package com.ims.core.doc.service.impl;

import java.util.List;
import java.util.Objects;

import com.ims.common.core.domain.entity.SysDept;
import com.ims.common.core.domain.entity.SysUser;
import com.ims.common.core.domain.model.LoginUser;
import com.ims.common.exception.ServiceException;
import com.ims.common.utils.DateUtils;
import com.ims.common.utils.SecurityUtils;
import com.ims.common.utils.uuid.IdUtils;
import com.ims.system.domain.TSysSite;
import com.ims.system.service.ITSysSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ims.core.doc.mapper.TInInvoiceFolderMapper;
import com.ims.core.doc.domain.TInInvoiceFolder;
import com.ims.core.doc.service.ITInInvoiceFolderService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 进项票夹Service业务层处理
 *
 * @author IMS
 * @date 2024-11-10
 */
@Service
public class TInInvoiceFolderServiceImpl implements ITInInvoiceFolderService
{


    @Autowired
    private TInInvoiceFolderMapper tInInvoiceFolderMapper;

    /**
     * 查询进项票夹
     *
     * @param id 进项票夹主键
     * @return 进项票夹
     */
    @Override
    public TInInvoiceFolder selectTInInvoiceFolderById(Long id)
    {
        return tInInvoiceFolderMapper.selectTInInvoiceFolderById(id);
    }

    /**
     * 查询进项票夹
     *
     * @param folderId 进项票夹ID
     * @return 进项票夹
     */
    @Override
    public TInInvoiceFolder selectTInInvoiceFolderByFolderId(String folderId)
    {
        return tInInvoiceFolderMapper.selectTInInvoiceFolderByFolderId(folderId);
    }

    /**
     * 查询进项票夹列表
     *
     * @param tInInvoiceFolder 进项票夹
     * @return 进项票夹
     */
    @Override
    public List<TInInvoiceFolder> selectTInInvoiceFolderList(TInInvoiceFolder tInInvoiceFolder)
    {
        return tInInvoiceFolderMapper.selectTInInvoiceFolderList(tInInvoiceFolder);
    }

    /**
     * 新增进项票夹
     *
     * @param tInInvoiceFolder 进项票夹
     * @return 结果
     */
    @Override
    public int insertTInInvoiceFolder(TInInvoiceFolder tInInvoiceFolder)
    {
        tInInvoiceFolder.setFolderId(IdUtils.simpleUUID());
        // 提交人
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 名称判重
        TInInvoiceFolder condition = new TInInvoiceFolder();
        condition.getParams().put("fullName", tInInvoiceFolder.getFolderName());
        condition.setSiteCode(loginUser.getSiteCode());
        List<TInInvoiceFolder> folderList = tInInvoiceFolderMapper.selectTInInvoiceFolderList(condition);
        if(!CollectionUtils.isEmpty(folderList)){
            throw new ServiceException("票夹重名，请修改重试");
        }
        tInInvoiceFolder.setSiteCode(loginUser.getSiteCode());
        tInInvoiceFolder.setSiteName(loginUser.getSiteName());
        tInInvoiceFolder.setCreateBy(loginUser.getUsername());
        tInInvoiceFolder.setCreateTime(DateUtils.getNowDate());
        return tInInvoiceFolderMapper.insertTInInvoiceFolder(tInInvoiceFolder);
    }

    /**
     * 修改进项票夹
     *
     * @param tInInvoiceFolder 进项票夹
     * @return 结果
     */
    @Override
    public int updateTInInvoiceFolder(TInInvoiceFolder tInInvoiceFolder)
    {
        // 提交人
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 名称判重
        TInInvoiceFolder condition = new TInInvoiceFolder();
        condition.getParams().put("fullName", tInInvoiceFolder.getFolderName());
        condition.setSiteCode(loginUser.getSiteCode());
        List<TInInvoiceFolder> folderList = tInInvoiceFolderMapper.selectTInInvoiceFolderList(condition);
        if(!CollectionUtils.isEmpty(folderList)){
            throw new ServiceException("票夹重名，请修改重试");
        }
        tInInvoiceFolder.setUpdateBy(loginUser.getUsername());
        tInInvoiceFolder.setUpdateTime(DateUtils.getNowDate());
        return tInInvoiceFolderMapper.updateTInInvoiceFolder(tInInvoiceFolder);
    }

    /**
     * 删除进项票夹信息
     *
     * @param id 进项票夹主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteTInInvoiceFolderById(Long id)
    {
        TInInvoiceFolder tInInvoiceFolder = tInInvoiceFolderMapper.selectTInInvoiceFolderById(id);
        if(tInInvoiceFolder.getIsDefault() == 1){
            throw new ServiceException("默认票夹无法删除！");
        }
        int res = tInInvoiceFolderMapper.deleteTInInvoiceFolderById(id);
        // 删除票夹
        if(res == 1) {
            // 释放票夹下面的发票
            tInInvoiceFolderMapper.releaseInInvoiceFromFolder(tInInvoiceFolder.getFolderId());
        }
        return res;
    }
}
